package com.ruoyi.framework.interceptor;

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.web.exception.SealException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: NH
 * @description: 拦截器实现封停数据库操作
 * @author: 郑吉志
 * @create: 2023-02-19 14:14
 **/
@Component
public class SealInterceptor extends HandlerInterceptorAdapter {
    public static Map<Integer,Boolean> FLAG=new HashMap<>();
    static {
        for (int i = 2000; i < 2100; i++) {
            FLAG.put(i,true);
        }
    }
    /**
     * This implementation always returns {@code true}.
     */
    // 在业务处理器处理请求之前被调用(对数据库进行操作之前)
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        SealException sealException=new SealException("数据停封禁止操作");
        int year= Integer.parseInt(request.getHeader("year"));
        System.out.println("拦截器：：：：："+year);

            System.out.println(FLAG.get(year));
            if (FLAG.get(year))
                return true;
            else
                throw sealException;
    }
}
